/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tkgarment.system.action;

import com.tkgarment.system.dao.EmployeeDAO;
import com.tkgarment.system.domain.Employee;
import com.tkgarment.system.factory.DAOFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author zraj
 */
public class SearchEmployeeJson extends ActionSupportBase implements SessionAware {


    private static final long serialVersionUID = 5078264277068533593L;
    private static final Log log = LogFactory.getLog(Employee.class);

    // Your result List
    private List<Employee> gridModel;
    private Map<String, Object> session;
    private List<Employee> myEmps;

    public String execute() {
        log.debug("Page " + getPage() + " Rows " + getRows() + " Sorting Order " + getSord() + " Index Row :" + getSidx());
        log.debug("Search :" + getSearchField() + " " + getSearchOper() + " " + getSearchString());
        System.out.println(getSearchString());
        Object list = session.get("mylist");
        if (list != null) {
            myEmps = (List<Employee>) list;
        } else {
            log.debug("Build new List");
            EmployeeDAO dao = DAOFactory.createEmployeeDao();
            try {
                if (getSearchString() != null && getSearchString().compareTo("") != 0) {
                   myEmps = dao.getByFilter(getSearchField(),getSearchString(),getSearchOper()); 
                }else{
                   myEmps = dao.getAll(); 
                }
                
//           myEmps =  new ArrayList<>();
//            for (int i = 0; i < 1; i++) {
//                Employee emp = new Employee();
//                emp.setEmpId("55-1234");
//                emp.setNameTH("ศุภเกียรติ");
//                emp.setSurnameTH("ทิพย์อักษร");
//                myEmps.add(emp);
//            }
            } catch (Exception ex) {
                Logger.getLogger(SearchEmployeeJson.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

//        if (sord != null && sord.equalsIgnoreCase("asc")) {
//            Collections.sort(myEmps);
//        }
//        if (sord != null && sord.equalsIgnoreCase("desc")) {
//            Collections.sort(myEmps);
//            Collections.reverse(myEmps);
//        }
        // Count all record (select count(*) from your_custumers)
        setRecords(myEmps.size());

        if (getTotalrows() != null) {
            setRecords(getTotalrows());
        }

        // Calucalate until rows ware selected
        int to = (getRows() * getPage());

        // Calculate the first row to read
        int from = to - getRows();

        // Set to = max rows
        if (to > getRecords()) {
            to = getRecords();
        }

        if (isLoadonce()) {
            if (getTotalrows() != null && getTotalrows() > 0) {
                setGridModel(myEmps.subList(0, getTotalrows()));
            } else {
                // All Custumer
                setGridModel(myEmps);
            }
        }

        // Calculate total Pages
        setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

        // only for showcase functionality, don't do this in production
       // session.put("mylist", myEmps);

        return SUCCESS;
    }

    
        @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

    
    
    public String getJSON() {
        return execute();
    }

  
    /**
     * @return an collection that contains the actual data
     */
    public List<Employee> getGridModel() {
        return gridModel;
    }

    /**
     * @param gridModel an collection that contains the actual data
     */
    public void setGridModel(List<Employee> gridModel) {
        this.gridModel = gridModel;
    }

   

    public List<Employee> getMyEmps() {
        return myEmps;
    }

    public void setMyEmps(List<Employee> myEmps) {
        this.myEmps = myEmps;
    }

    
    
}


